package org.finalministry.database.dao;

import java.util.List;

import org.finalministry.objects.Congregation;

public class CongregationDAO extends AbstractDAO {

	private static final String TABLENAME = "congregation";

	public CongregationDAO() {
		super();
	}

	/**
	 * Insert a new Congregation into the database.
	 * 
	 * @param Congregation
	 * @return persisted ID
	 * @throws DataAccessLayerException
	 */
	public Congregation merge(Congregation congregation) throws DataAccessLayerException {

		return (Congregation) super.merge(congregation);
	}

	/**
	 * Delete a detached Congregation from the database.
	 * 
	 * @param Congregation
	 */
	public void delete(Congregation congregation) throws DataAccessLayerException {

		super.delete(congregation);
	}

	/**
	 * Find an Congregation by its primary key.
	 * 
	 * @param id
	 * @return
	 */
	public Congregation find(Long id) throws DataAccessLayerException {

		return (Congregation) super.find(Congregation.class, id);
	}

	/**
	 * Finds all Congregations in the database.
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Congregation> findAll() throws DataAccessLayerException {

		return super.findAll(Congregation.class);
	}

	/**
	 * Get table name
	 */
	@Override
	protected String getTableName() {

		return TABLENAME;
	}

	// public Long create(Congregation congregation) throws
	// ClassNotFoundException, Exception {
	// String insert =
	// "INSERT INTO CONGREGATION (NAME,LANGUAGEID,ADDRESSID) VALUES(?,?,?)";
	// PreparedStatement stmt =
	// getConnection().getConnection().prepareStatement(insert);
	//
	// Long congregationId =
	// DaoFactory.getInstance().getCongregationDAO().create(congregation.getCongregation());
	//
	// stmt.setString(1, congregation.getName());
	// stmt.setLong(2, congregation.getLanguage().getId());
	// stmt.setLong(3, congregationId);
	//
	// stmt.executeUpdate();
	//
	// stmt.close();
	//
	// return lastGeneratedId();
	// }
	//
	// public Congregation getById(Long id) throws SQLException,
	// ClassNotFoundException, ParseException {
	//
	// Statement stmt = getConnection().getConnection().createStatement();
	// ResultSet result =
	// stmt.executeQuery("SELECT ID,NAME,LANGUAGEID,ADDRESSID FROM CONGREGATION WHERE ID = "
	// + id);
	//
	// if (result.next()) {
	// Congregation congregation = new Congregation();
	// congregation.setId(result.getLong(1));
	// congregation.setName(result.getString(2));
	// congregation.setLanguage(DaoFactory.getInstance().getCountryDAO().getById(result.getLong(3)));
	// congregation.setCongregation(DaoFactory.getInstance().getCongregationDAO().getById(result.getLong(4)));
	// return congregation;
	// }
	// return null;
	// }
	//
	// public List<Congregation> getAll() throws SQLException,
	// ClassNotFoundException, ParseException {
	//
	// Statement stmt = getConnection().getConnection().createStatement();
	// String select = "SELECT * FROM CONGREGATION";
	//
	// return standardSelectCongregationList(select, stmt);
	// }

	/**
	 * Convinient method for return a list of congregations via a simple
	 * executeQuery from
	 * 
	 * <pre>
	 * SELECT * FROM CONGREGATION ...
	 * </pre>
	 * 
	 * @param select
	 * @param stmt
	 * @return
	 * @throws SQLException
	 * @throws ParseException
	 * @throws ClassNotFoundException
	 */
	// private List<Congregation> standardSelectCongregationList(String select,
	// Statement stmt) throws SQLException, ClassNotFoundException,
	// ParseException {
	//
	// ResultSet result = stmt.executeQuery(select);
	// List<Congregation> allCongregations = new ArrayList<Congregation>();
	//
	// while (result.next()) {
	// Congregation congregation = new Congregation();
	// congregation.setId(result.getLong(1));
	// congregation.setName(result.getString(2));
	// congregation.setLanguage(DaoFactory.getInstance().getCountryDAO().getById(result.getLong(3)));
	// congregation.setCongregation(DaoFactory.getInstance().getCongregationDAO().getById(result.getLong(4)));
	//
	// allCongregations.add(congregation);
	// }
	//
	// return allCongregations;
	// }
	//
	// public void update(Congregation congregation) throws Exception {
	//
	// String insert =
	// "UPDATE CONGREGATION SET NAME = ?, LANGUAGEID = ?, ADDRESSID = ? WHERE ID = ?";
	// PreparedStatement stmt =
	// getConnection().getConnection().prepareStatement(insert);
	//
	// Long newCongregationId =
	// DaoFactory.getInstance().getCongregationDAO().create(congregation.getCongregation());
	//
	// stmt.setString(1, congregation.getName());
	// stmt.setLong(2, congregation.getLanguage().getId());
	// stmt.setLong(3, newCongregationId);
	// stmt.setLong(4, congregation.getId());
	//
	// stmt.executeUpdate();
	//
	// stmt.close();
	//
	// }
	//
	// public void delete(Congregation congregation) throws Exception {
	//
	// String delete = "DELETE FROM CONGREGATION WHERE ID = " +
	// congregation.getId();
	// Statement stmt = getConnection().getConnection().createStatement();
	// stmt.executeUpdate(delete);
	// stmt.close();
	// }
	//
	// private Long lastGeneratedId() throws SQLException,
	// ClassNotFoundException {
	// Statement stmt = getConnection().getConnection().createStatement();
	// ResultSet result = stmt.executeQuery("SELECT MAX(ID) FROM CONGREGATION");
	//
	// if (result.next()) {
	// return result.getLong(1);
	// }
	// return null;
	// }
}
